package com.alibaba.citrus.cr.sales.plan.repository;

import com.alibaba.citrus.cr.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.cr.sales.plan.dto.api.request.QuerySalesPlanAuditRequest;
import com.alibaba.citrus.cr.sales.plan.sdo.SalesPlanAuditSDO;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcorder.sales_plan.salesplanaudit.dto.CreateSalesPlanAuditResponse;
import com.epoch.app.bcorder.sales_plan.salesplanaudit.dto.LoadSalesPlanAuditListRequest;
import com.epoch.app.bcorder.sales_plan.salesplanaudit.dto.LoadSalesPlanAuditRequest;
import com.epoch.app.bcorder.sales_plan.salesplanaudit.dto.UpdateSalesPlanAuditResponse;
import com.epoch.app.bcorder.sales_plan.salesplanaudit.model.SalesPlanAudit;
import com.epoch.app.bcorder.sales_plan.salesplanaudit.service.SalesPlanAuditService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Component
public class SalesPlanReportAuditRepositoryImpl implements SalesPlanAuditRepository {

    @Resource
    private SalesPlanAuditService salesPlanAuditService;

    @Override
    @RepositoryInvoker(printLog = true)
    public String save(SalesPlanAuditSDO salesPlanAuditSDO) {
        SalesPlanAudit salesPlanAudit = new SalesPlanAudit();
        BeanUtils.copyProperties(salesPlanAuditSDO, salesPlanAudit);
        salesPlanAudit.setTenantId("tny");
        salesPlanAudit.setGmtCreate(new Date());
        salesPlanAudit.setPlanId(Long.valueOf(salesPlanAuditSDO.getPlanId()));
        salesPlanAudit.setCreatorId(salesPlanAuditSDO.getCreatorId());
        salesPlanAudit.setReportOrderId(Long.valueOf(salesPlanAuditSDO.getReportOrderId()));
        CreateSalesPlanAuditResponse salesPlanAuditResponse = salesPlanAuditService.createSalesPlanAudit(salesPlanAudit);
        return salesPlanAuditResponse.getLastInsertId();
    }

    @Override
    @RepositoryInvoker(printLog = true)
    public Boolean edit(SalesPlanAuditSDO salesPlanAuditSDO) {
        SalesPlanAudit salesPlanAudit = new SalesPlanAudit();
        BeanUtils.copyProperties(salesPlanAuditSDO, salesPlanAudit);
        salesPlanAudit.setReportOrderId(Optional.ofNullable(salesPlanAuditSDO.getReportOrderId()).map(id -> Long.parseLong(salesPlanAuditSDO.getReportOrderId())).orElse(null));
        salesPlanAudit.setId(Long.valueOf(salesPlanAuditSDO.getId()));
        salesPlanAudit.setGmtModified(new Date());
        salesPlanAudit.setAuditInitiatorId(Optional.ofNullable(salesPlanAuditSDO.getAuditInitiatorId()).map(s -> s.replace("CZ_", "")).orElse(null));
        UpdateSalesPlanAuditResponse updateSalesPlanAuditResponse = salesPlanAuditService.updateSalesPlanAudit(salesPlanAudit);
        return updateSalesPlanAuditResponse.getCount() > 0;
    }

    @Override
    @RepositoryInvoker(printLog = true)
    public List<SalesPlanAuditSDO> querySalesPlanAuditList(QuerySalesPlanAuditRequest querySalesPlanAuditRequest) {
        LoadSalesPlanAuditListRequest loadSalesPlanAuditListRequest = new LoadSalesPlanAuditListRequest();
        List<String> reportOrderIdList = querySalesPlanAuditRequest.getReportOrderIdList();
        loadSalesPlanAuditListRequest.setReportOrderIdList(reportOrderIdList);
        loadSalesPlanAuditListRequest.setPlanId(Optional.ofNullable(querySalesPlanAuditRequest.getPlanId()).map(Long::parseLong).orElse(null));
        loadSalesPlanAuditListRequest.setId(Optional.ofNullable(querySalesPlanAuditRequest.getId()).map(Long::parseLong).orElse(null));
        loadSalesPlanAuditListRequest.setDir("gmtCreate");
        loadSalesPlanAuditListRequest.setAsc(false);
        loadSalesPlanAuditListRequest.setStart(0);
        loadSalesPlanAuditListRequest.setLimit(100);
        Result<List<SalesPlanAudit>> listResult = salesPlanAuditService.loadSalesPlanAuditList(loadSalesPlanAuditListRequest);
        List<SalesPlanAudit> result = listResult.getResult();
        if (CollectionUtils.isEmpty(result)) {
            return Collections.emptyList();
        }
        return result.stream().map(this::convertSalesPlanAuditSDO).collect(Collectors.toList());
    }

    @Override
    @RepositoryInvoker(printLog = true)
    public SalesPlanAuditSDO load(QuerySalesPlanAuditRequest querySalesPlanAuditRequest) {
        LoadSalesPlanAuditRequest loadSalesPlanAuditRequest = new LoadSalesPlanAuditRequest();
        loadSalesPlanAuditRequest.setId(querySalesPlanAuditRequest.getId());
        SalesPlanAudit salesPlanAudit = salesPlanAuditService.loadSalesPlanAudit(loadSalesPlanAuditRequest);
        SalesPlanAuditSDO salesPlanAuditSDO = new SalesPlanAuditSDO();
        BeanUtils.copyProperties(salesPlanAudit, salesPlanAuditSDO);
        salesPlanAuditSDO.setPlanId(String.valueOf(salesPlanAudit.getPlanId()));
        salesPlanAuditSDO.setId(String.valueOf(salesPlanAudit.getId()));
        salesPlanAuditSDO.setReportOrderId(String.valueOf(salesPlanAudit.getReportOrderId()));
        return salesPlanAuditSDO;
    }

    private SalesPlanAuditSDO convertSalesPlanAuditSDO(SalesPlanAudit salesPlanAudit) {
        if (null == salesPlanAudit) {
            return null;
        }
        SalesPlanAuditSDO salesPlanAuditSDO = new SalesPlanAuditSDO();
        salesPlanAuditSDO.setId(String.valueOf(salesPlanAudit.getId()));
        salesPlanAuditSDO.setGmtCreate(salesPlanAudit.getGmtCreate());
        salesPlanAuditSDO.setGmtModified(salesPlanAudit.getGmtModified());
        salesPlanAuditSDO.setCreatorId(salesPlanAudit.getCreatorId());
        salesPlanAuditSDO.setModifierId(salesPlanAudit.getModifierId());
        salesPlanAuditSDO.setPlanId(String.valueOf(salesPlanAudit.getPlanId()));
        salesPlanAuditSDO.setPlanTime(salesPlanAudit.getPlanTime());
        salesPlanAuditSDO.setReportOrderId(String.valueOf(salesPlanAudit.getReportOrderId()));
        salesPlanAuditSDO.setFlowInstanceId(salesPlanAudit.getFlowInstanceId());
        salesPlanAuditSDO.setProcessTitle(salesPlanAudit.getProcessTitle());
        salesPlanAuditSDO.setAuditStatus(salesPlanAudit.getAuditStatus());
        salesPlanAuditSDO.setAuditReason(salesPlanAudit.getAuditReason());
        salesPlanAuditSDO.setAuditFinishTime(salesPlanAudit.getAuditFinishTime());
        salesPlanAuditSDO.setAuditInitiatorId(salesPlanAudit.getAuditInitiatorId());
        salesPlanAuditSDO.setAuditUserId(salesPlanAudit.getAuditUserId());
        salesPlanAuditSDO.setFeatures(salesPlanAudit.getFeatures());
        return salesPlanAuditSDO;
    }
}
